//
//  FFPayCenter.m
//  Components
//
//  Created by IT.Bear on 16/7/7.
//  Copyright (c) 2016年 fashionfhinger. All rights reserved.
//

#import "FFPayCenter.h"
#import "FFMacro.h"

#import "FFAlixpayHandler.h"
#import "FFWeChatHandler.h"
//#import "FFUnionPayHandler.h"
#import <AlipaySDK/AlipaySDK.h>
#import "WXApi.h"
#import <UIKit/UIKit.h>
//#import "FFMimeHandler.h"

@interface FFPayCenter()<FFAlixpayDelegate,FFWeChatDelegate,WXApiDelegate/*,FFUnionPayDelegate*/>
@property (nonatomic, copy) PayAction callblock;
@end

@implementation FFPayCenter

DECLARE_SINGLETON(FFPayCenter)

- (id<FFPayHandler>)buildPayHandlerWith:(FFPayInfo *)payInfo viewController:(UIViewController *)fromVC{
    id<FFPayHandler> handler;
    if ([payInfo.paymentID isEqualToString:@"1"] || [payInfo.paymentID isEqualToString:@"4"]) {
        /*!
         *  支付宝
         */
      
        FFAlixpayHandler *alixpayHandler = [[FFAlixpayHandler alloc] initWithPayInfo:payInfo];
        
        alixpayHandler.delegate = self;
        handler = alixpayHandler;
        
    }else if ([payInfo.paymentID isEqualToString:@"2"]) {
        //微信支付
        FFWeChatHandler *weChatHandler = [[FFWeChatHandler alloc] initWithPayInfo:payInfo];
        weChatHandler.delegate = self;
        handler = weChatHandler;
    }else if ([payInfo.paymentID isEqualToString:@"3"]){
//        FFMimeHandler *mimeHandler = [[FFMimeHandler alloc] initWithPayInfo:payInfo];
//        mimeHandler.delegate = self;
//        if (fromVC) {
//            mimeHandler.from_VC = fromVC;
//        }
//        handler = mimeHandler;
    }else if ([payInfo.paymentID isEqualToString:@"5"]) {
        //银联支付
//        FFUnionPayHandler *unionPayHandler = [[FFUnionPayHandler alloc] initWithPayInfo:payInfo];
//        if(fromVC){
//            unionPayHandler.fromVC = fromVC;
//        }
//        unionPayHandler.delegate = self;
//        handler = unionPayHandler;
    }
    return handler;
}

- (id)init{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)setResultAction:(PayAction)block{
    if (block) {
        self.callblock = block;
    }
}

#pragma mark -- 处理支付宝回调
- (void)handleAlixpayURL:(NSURL *)url{
    __weak FFPayCenter * weakSelf = self;
    [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
        if (resultDic) {
            if ([resultDic[@"resultStatus"] intValue] == 9000) {
                //成功
                if (weakSelf.callblock) {
                    weakSelf.callblock(FFPayResultAlixpaySuccess);
                }
            }else{
                //失败
                if (weakSelf.callblock) {
                    weakSelf.callblock(FFPayResultAlixpayFailed);
                }
            }
        }else{
            //失败
            if (_callblock) {
                _callblock(FFPayResultAlixpayFailed);
            }
        }
    }];
}

#pragma mark - FFAlixpayDelegate
- (void)alixpayHandler:(FFAlixpayHandler *)handler payResult:(FFPayResult)result{
    if (_callblock) {
        _callblock(result);
    }
}



#pragma mark - 处理微信支付回调
- (void)handleWeChatURL:(NSURL *)url {
    [WXApi handleOpenURL:url delegate:self];
}

#pragma mark - WXApiDelegate
-(void) onResp:(BaseResp*)resp{
    if ([resp isKindOfClass:[PayResp class]]) {
        PayResp *response = (PayResp *)resp;
        switch (response.errCode) {
            case WXSuccess:
            {
                if (_callblock) {
                    _callblock(FFPayResultWeChatPaySuccess);
                }
            }
                break;
            default:
            {
                if (_callblock) {
                    _callblock(FFPayResultWeChatPayFailed);
                }
            }
                break;
        }
    }
}

#pragma mark - FFWeChatDelegate
- (void)weChatHandler:(FFWeChatHandler *)handler payResult:(FFPayResult)result {
    if (_callblock) {
        _callblock(result);
    };
}

#pragma mark - FFUnionPayDelegate
//- (void)unionPayHandler:(FFUnionPayHandler *)handler payResult:(FFPayResult)result {
//    if (_callblock) {
//        _callblock(result);
//    };
//}

#pragma mark - FFMimeHandlerDelegate
//- (void)mimePayHandler:(FFMimeHandler *)handler payResult:(FFPayResult)result
//{
//    if (_callblock) {
//        _callblock(result);
//    }
//}


//#pragma mark - FFGaoCoinPayDelegate
//- (void)gaoCoinPayHandler:(FFGaoCoinPayHandler *)handler payResult:(FFPayResult)result message:(NSString *)message activityId:(NSString *)activityId{
//    if (_callblock) {
//        _callblock(result);
//    };
//}
//
//#pragma mark - FFLifeUnionPayDelegate
//- (void)lifeUnionPayHandler:(FFLifeUnionPayHandler *)handler payResult:(FFPayResult)result {
//    if (_callblock) {
//        _callblock(result);
//    };
//}
@end
